package remote.controls
{
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import remote.ISelect;
	import remote.enums.EnableStatus;
	import remote.enums.HAlgin;
	import remote.enums.SelectStatus;
	import remote.enums.VAlgin;
	import remote.geom.EdgeMetrics;
	import remote.geom.Size;
	import remote.layouts.HorizontalLayout;
	import remote.skins.ButtonSkin;
	
	[Event(name="change", type="flash.events.Event")]
	/**
	 * 按钮
	 * @author 银魂飞雪
	 * @createDate 2011-2-26
	 */
	public class Button extends Component implements ISelect
	{
		public static function createStatusString(mouseStatus:String, enable:Boolean, selected:Boolean):String
		{
			return mouseStatus + (enable? EnableStatus.ENABLE : EnableStatus.UNABLE) + (selected? SelectStatus.SELECTED : SelectStatus.UNSELECTED);
		}
		
		private var _textField:Text;
		
		protected var _selected:Boolean;
		protected var _toggle:Boolean = false;
		
		public function Button()
		{ 
			super();
		}
		
		/**
		 * @inheritDoc 
		 * 
		 */	
		override protected function initProperty():void
		{
			super.initProperty();
			_width = 60;
			_height = 24;
			mouseChildren = false;
			buttonMode = true;
			useHandCursor = true;
		}
		
		/**
		 * @inheritDoc 
		 * 
		 */	
		override protected function updateChildren():void
		{
			var edge:EdgeMetrics = _layout.edge;
			_textField.setSize(width - edge.left - edge.right, height - edge.top - edge.bottom);
			var txtMearsureSize:Size = _textField.getMeasureSize();				//如果文本框的尺寸大于合适的尺寸，则设为合适的尺寸
			if(_textField.width > txtMearsureSize.width)
			{
				_textField.width = txtMearsureSize.width;
			}
			if(_textField.height > txtMearsureSize.height)
			{
				_textField.height = txtMearsureSize.height;
			}
			updateLayout();
		}
		
		/**
		 * @inheritDoc 
		 * 
		 */	
		override protected function createChildren():void
		{
			_textField = new Text();
			_textField.width = _textField.height = 0;
			addChild(_textField);
		}
		
		/**
		 * @inheritDoc 
		 * 
		 */	
		override protected function createLayout():void
		{
			_layout = new HorizontalLayout(this, 0, VAlgin.MIDDLE, HAlgin.CENTER, 5, 3, 5, 3);
		}
		
		/**
		 * @inheritDoc 
		 * 
		 */	
		override protected function createSkin():void
		{
			_skin = new ButtonSkin();
		}
		
		/**
		 * @inheritDoc 
		 * 
		 */	
		override public function updateSize():void
		{
			var oldSize:Size = new Size(width, height);
			super.updateSize();
			if(!oldSize.equal(new Size(width, height)))
				this.updateChildren();
		}
		
		/**
		 * @inheritDoc 
		 * 
		 */	
		override public function getMeasureSize():Size
		{
			var oldSize:Size = new Size(_textField.width, _textField.height);
			var edge:EdgeMetrics = _layout.edge;
			if(isNaN(this._explicitWidth))
				_textField.width = NaN;
			else
				_textField.height = this._explicitWidth - edge.left - edge.right;
			if(isNaN(this._explicitHeight))
				_textField.height = NaN;
			else
				_textField.height = this._explicitHeight - edge.top - edge.bottom;
			
			var tfSize:Size = _textField.getMeasureSize();
			var result:Size = new Size(tfSize.width + edge.left + edge.right, tfSize.height + edge.top + edge.bottom);
			_textField.setSize(oldSize.width, oldSize.height);
			
			return result;
			
		}
		
		protected function clickHandler(e:MouseEvent):void
		{
			selected = !selected;
		}
		
		/**
		 * @inheritDoc 
		 * 
		 */	
		override public function setMouseStatus(value:String):void
		{
			var oldValue:String = mouseStatus;
			super.setMouseStatus(value);
			if(oldValue != mouseStatus)
				updateSkin();
		}
		
		protected function updateToggle():void
		{
			if(_toggle == true)
				addEventListener(MouseEvent.CLICK, clickHandler);
			else
				removeEventListener(MouseEvent.CLICK, clickHandler);
		}
		/**
		 * 文本标签
		 * 
		 */		
		public function set label(value:String):void
		{
			if(value == null)
				value = "";
			if(label != value)
			{
				var oldLabel:String = label;
				_textField.text = value;
				this.updateSize();
				this.updateChildren();
			}
		}
		
		/**
		 * 按钮上显示的标签文字 
		 * 
		 */		
		public function get label():String
		{
			return _textField.text;
		}
		
		/**
		 * @inheritDoc 
		 * 
		 */	
		public function get selected():Boolean
		{
			return _selected;
		}

		public function set selected(value:Boolean):void
		{
			if(_selected != value)
			{
				_selected = value;
				updateSkin();
				if(this.hasEventListener(Event.CHANGE))
					this.dispatchEvent(new Event(Event.CHANGE));
			
			}
		}

		/**
		 * 是否为切换式按钮
		 * 
		 */		
		public function get toggle():Boolean
		{
			return _toggle;
		}

		public function set toggle(value:Boolean):void
		{
			if(_toggle != value)
			{
				_toggle = value;
				updateToggle();
			}
		}
		
		/**
		 * @inheritDoc 
		 * 
		 */	
		override public function get skinStatus():String
		{
			return Button.createStatusString(this.mouseStatus, this.enable, this.selected);
		}


	}
}